import random
from datetime import datetime

# 商品列表
products = {
    "空调": 3000,
    "电视": 2000,
    "冰箱": 2500,
    "洗衣机": 1500,
    "微波炉": 500,
    "电饭煲": 300
}

# 优惠券列表
coupons = {
    "空调": 0.8,
    "电视": 0.9,
    "冰箱": 0.85,
    "洗衣机": 0.95,
    "微波炉": 0.8,
    "电饭煲": 0.74
}

# 用户账户余额
money = 5000

# 购物车
shopping_cart = []


# 抽取优惠券
def draw_coupon():
    coupon_product = random.choice(list(coupons.keys()))
    discount = coupons[coupon_product]
    print(f"恭喜您！您抽到了{coupon_product}的{int(discount * 100)}折优惠券！")
    return coupon_product, discount


# 打印购物小条
def print_receipt(sc, balance):
    print("\n----------------购物小条----------------")
    print(f"购物时间：{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"卡余额：{balance}元")
    print(f"购物数量：{len(sc)}件")
    total_value = sum(item["price"] for item in sc)
    print(f"购买总商品价值：{total_value}元")
    print("购物清单：")
    for item in sc:
        print(f"商品：{item['name']}，价格：{item['price']}元")
    print("----------------------------------------")


# 购物功能
def shopping():
    global money
    coupon_product, discount = draw_coupon()
    print("\n欢迎进入商场！开始购物吧！")
    print("当前商品列表：")
    for product, price in products.items():
        print(f"{product}：{price}元")

    while True:
        user_input = input("\n请输入要购买的商品名称（输入'Q'或'q'退出购物）：")
        if user_input.lower() == 'q':
            print_receipt(shopping_cart, money)
            break

        if user_input not in products:
            print("没有这个商品，别瞎弄！")
            continue

        if user_input == coupon_product:
            price = products[user_input] * discount
            print(f"您使用了{coupon_product}的优惠券，价格为{price:.2f}元！")
        else:
            price = products[user_input]

        if money >= price:
            shopping_cart.append({"name": user_input, "price": price})
            money -= price
            print(f"购买成功！您的卡余额还剩{money}元。")
        else:
            print("穷鬼，钱不够！请到其他超市购买！")

        # 退货功能
        while True:
            return_input = input("是否需要退货？（Y/N）：")
            if return_input.lower() == 'y':
                if not shopping_cart:
                    print("购物车为空，无法退货！")
                    break
                print("当前购物车商品：")
                for idx, item in enumerate(shopping_cart):
                    print(f"{idx + 1}. {item['name']} - {item['price']}元")
                return_index = int(input("请输入要退货的商品编号：")) - 1
                if 0 <= return_index < len(shopping_cart):
                    return_item = shopping_cart.pop(return_index)
                    money += return_item["price"]
                    print(
                        f"退货成功！商品{return_item['name']}已退回，卡余额增加{return_item['price']}元，当前余额为{money}元。")
                else:
                    print("无效的商品编号！")
                break
            elif return_input.lower() == 'n':
                break
            else:
                print("无效输入，请输入Y或N！")


# 开始购物
shopping()
